/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Course;
import db.dao.CourseDAO;
import db.dao.StudentDAO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * Lists the courses of the currently logged in student.
 *
 * @author jonas
 */
public class CourseDataTableBean{
    private ArrayList<Course> courses;
    private ArrayList<Course> currentCourses;
    private int headerCnt;
    //private String currHeader;

    private String searchName;
    private String searchCode;

    public static int COLUMN_COUNT = 3;
    private String[] headers = new String[COLUMN_COUNT];

    public String getCurrHeader(){
        String header = headers[headerCnt];
        headerCnt++;
        headerCnt %= COLUMN_COUNT;
        return header;
    }

    @PostConstruct
    public void init(){
        headers[0] = "Name";
        headers[1] = "Code";
        headers[2] = "Description";

        courses = new ArrayList();
        currentCourses = new ArrayList();

        searchName = "";
        searchCode = "";
    }

    public List<Course> getCourses(){
        courses.clear();
        courses = (ArrayList)CourseDAO.search(new Course(0,searchName,searchCode,""));
        //(ArrayList)CourseDAO.readAll();
        return courses;
    }
    
    public void setCourses(ArrayList<Course> courses){
        this.courses = courses;
    }

    public List<Course> getCurrentCourses() {
        //Get active login id by getting login session bean
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        currentCourses.clear();
        currentCourses = (ArrayList)StudentDAO.getCurrentCourses(lb.getId());
        return currentCourses;
    }

    public void setCurrentCourses(ArrayList<Course> currentCourses) {
        this.currentCourses = currentCourses;
    }

    public String getSearchCode() {
        return searchCode;
    }

    public void setSearchCode(String searchCode) {
        this.searchCode = searchCode;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }
}
